home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / fax / src / libtiff / mkg3states.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  26KB  |  826 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/libtiff/RCS/mkg3states.c,v 1.15 93/02/14 10:15:25 sam Rel $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1991, 1992 Sam Leffler
  7.  * Copyright (c) 1991, 1992 Silicon Graphics, Inc.
  8.  *
  9.  * Permission to use, copy, modify, distribute, and sell this software and 
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that (i) the above copyright notices and this permission notice appear in
  12.  * all copies of the software and related documentation, and (ii) the names of
  13.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Sam Leffler and Silicon Graphics.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  20.  * 
  21.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  22.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  23.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  25.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  26.  * OF THIS SOFTWARE.
  27.  */
  28.  
  29. /*
  30.  * Program to construct Group 3 & Group 4 decoding tables.
  31.  *
  32.  * This code is derived from code by Michael P. Marking.  In
  33.  * particular, the algorithms to generate the null_mode and
  34.  * horiz_mode state tables are his.  See the comments below
  35.  * for more information.
  36.  *
  37.  * BEGIN (from the original source)
  38.  LEGAL
  39.  *    Copyright 1989, 1990 Michael P. Marking, Post Office Box 8039,
  40.  *    Scottsdale, Arizona 85252-8039. All rights reserved.
  41.  *
  42.  *    License is granted by the copyright holder to distribute and use this
  43.  *    code without payment of royalties or the necessity of notification as
  44.  *    long as this notice (all the text under "LEGAL") is included.
  45.  *
  46.  *    Reference: $Id: mkg3states.c,v 1.15 93/02/14 10:15:25 sam Rel $
  47.  *
  48.  *    This program is offered without any warranty of any kind. It includes
  49.  *    no warranty of merchantability or fitness for any purpose. Testing and
  50.  *    suitability for any use are the sole responsibility of the user.
  51.  *
  52.  INFORMATION
  53.  *    Although there is no support offered with this program, the author will
  54.  *    endeavor to correct errors. Updates will also be made available from
  55.  *    time to time.
  56.  *
  57.  *    Contact: Michael P. Marking, Post Office Box 8039, Scottsdale, Arizona
  58.  *    85252-8039 USA. Replies are not guaranteed to be swift. Beginning
  59.  *    July 1990, e-mail may be sent to uunet!ipel!marking.
  60.  *
  61.  *    Also beginning in July 1990, this code will be archived at the
  62.  *    ipel!phoenix BBS in file g3g4.zoo. The 24-hour telephone number
  63.  *    for 300/1200/2400 is (602)274-0462. When logging in, specify user
  64.  *    "public", system "bbs", and password "public".
  65.  *
  66.  *    This code is also available from the C Users Group in volume 317.
  67.  *
  68.  * END (from the original source)
  69.  */
  70. #include <stdio.h>
  71. #include "tiffcomp.h"
  72.  
  73. #ifndef TRUE
  74. #define    TRUE    1
  75. #define    FALSE    0
  76. #endif
  77.  
  78. #define WHITE    0
  79. #define BLACK    1
  80.  
  81. /*
  82.  * G3 2D and G4 decoding modes.  Note that
  83.  * the vertical modes are ordered so that
  84.  * (mode - MODE_VERT_V0) gives the vertical
  85.  * adjustment for the b1 parameter.
  86.  */
  87. #define MODE_NULL    0
  88. #define MODE_PASS    1
  89. #define MODE_HORIZ    2
  90. #define MODE_VERT_VL3    3
  91. #define MODE_VERT_VL2    4
  92. #define MODE_VERT_VL1    5
  93. #define MODE_VERT_V0    6
  94. #define MODE_VERT_VR1    7
  95. #define MODE_VERT_VR2    8
  96. #define MODE_VERT_VR3    9
  97. #define MODE_UNCOMP    10
  98. #define MODE_ERROR    11
  99. #define MODE_ERROR_1    12
  100.  
  101. unsigned long
  102. DECLARE1(append_0, unsigned long, prefix)
  103. {
  104.     return (prefix + (1L<<16));
  105. }
  106.  
  107. unsigned long
  108. DECLARE1(append_1, unsigned long, prefix)
  109. {
  110.     static unsigned short prefix_bit[16] = {
  111.     0x8000, 0x4000, 0x2000, 0x1000,
  112.     0x0800, 0x0400, 0x0200, 0x0100,
  113.     0x0080, 0x0040, 0x0020, 0x0010,
  114.     0x0008, 0x0004, 0x0002, 0x0001
  115.     };
  116.     unsigned char len = (prefix >> 16) & 0xf;
  117.     return (append_0(prefix) + prefix_bit[len]);
  118. }
  119.  
  120. #define    G3CODES
  121. #include "t4.h"
  122.  
  123. short
  124. DECLARE3(search_table, unsigned long, prefix, const tableentry*, tab, int, n)
  125. {
  126.     unsigned short len = (prefix >> 16) & 0xf;
  127.     unsigned short code = (prefix & 0xffff) >> (16 - len);
  128.  
  129.     while (n-- > 0) {
  130.     if (tab->length == len && tab->code == code)
  131.         return ((short) tab->runlen);
  132.     tab++;
  133.     }
  134.     return (G3CODE_INCOMP);
  135. }
  136.  
  137. #define    NCODES(a)    (sizeof (a) / sizeof (a[0]))
  138. short
  139. DECLARE1(white_run_length, unsigned long, prefix)
  140. {
  141.     return (search_table(prefix, TIFFFaxWhiteCodes, NCODES(TIFFFaxWhiteCodes)));
  142. }
  143.  
  144. short
  145. DECLARE1(black_run_length, unsigned long, prefix)
  146. {
  147.     return (search_table(prefix, TIFFFaxBlackCodes, NCODES(TIFFFaxBlackCodes)));
  148. }
  149. #undef NCODES
  150.  
  151. #define MAX_NULLPREFIX    200    /* max # of null-mode prefixes */
  152. typedef    unsigned char NullModeTable[MAX_NULLPREFIX][256];
  153. #define MAX_HORIZPREFIX    250    /* max # of incomplete 1-D prefixes */
  154. typedef    unsigned char HorizModeTable[MAX_HORIZPREFIX][256];
  155.  
  156.   /* the bit string corresponding to this row of the decoding table */
  157. long    null_mode_prefix[MAX_NULLPREFIX];
  158. NullModeTable null_mode;        /* MODE_*, indexed by bit and byte */
  159. NullModeTable null_mode_next_state;    /* next row of decoding tables to use */
  160.   /* number of prefixes or rows in the G4 decoding tables */
  161. short    null_mode_prefix_count = 0;
  162.  
  163. /*
  164.  * 2D uncompressed mode codes.  Note
  165.  * that two groups of codes are arranged
  166.  * so that the decoder can caluclate the
  167.  * length of the run by subtracting the
  168.  * code from a known base value.
  169.  */
  170. #define    UNCOMP_INCOMP    0
  171. /* runs of [0]*1 */
  172. #define    UNCOMP_RUN0    1
  173. #define    UNCOMP_RUN1    2
  174. #define    UNCOMP_RUN2    3
  175. #define    UNCOMP_RUN3    4
  176. #define    UNCOMP_RUN4    5
  177. #define    UNCOMP_RUN5    6
  178. #define    UNCOMP_RUN6    7
  179. /* runs of [0]* w/ terminating color */
  180. #define    UNCOMP_TRUN0    8
  181. #define    UNCOMP_TRUN1    9
  182. #define    UNCOMP_TRUN2    10
  183. #define    UNCOMP_TRUN3    11
  184. #define    UNCOMP_TRUN4    12
  185. /* special code for unexpected EOF */
  186. #define    UNCOMP_EOF    13
  187. /* invalid code encountered */
  188. #define    UNCOMP_INVALID    14
  189.  
  190. long    uncomp_mode_prefix[MAX_NULLPREFIX];
  191. NullModeTable uncomp_mode;
  192. NullModeTable uncomp_mode_next_state;
  193. short    uncomp_mode_prefix_count = 0;
  194.  
  195. /*
  196.  * Decoding action values for horiz_mode.
  197.  */
  198. #define ACT_INCOMP    0        /* incompletely decoded code */
  199. #define ACT_INVALID    1        /* invalide code */
  200. #define    ACT_WRUNT    2        /* terminating white run code */
  201. #define    ACT_WRUN    65        /* non-terminating white run code */
  202. #define    ACT_BRUNT    106        /* terminating black run code */
  203. #define    ACT_BRUN    169        /* non-terminating black run code */
  204. #define ACT_EOL        210        /* end-of-line code */
  205. HorizModeTable horiz_mode;
  206.  
  207. short
  208. DECLARE1(horiz_mode_code_black, short, runlen)
  209. {
  210.     return (runlen < 64 ? runlen + ACT_BRUNT : (runlen / 64) + ACT_BRUN);
  211. }
  212.  
  213. short
  214. DECLARE1(horiz_mode_code_white, short, runlen)
  215. {
  216.     return (runlen < 64 ? runlen + ACT_WRUNT : (runlen / 64) + ACT_WRUN);
  217. }
  218.  
  219. /*
  220.  * If the corresponding horiz_mode entry is ACT_INCOMP
  221.  * this entry is a row number for decoding the next byte;
  222.  * otherwise, it is the bit number with which to continue
  223.  * decoding the next codeword.
  224.  */
  225. HorizModeTable horiz_mode_next_state;
  226.         /* prefixes corresponding to the rows of the decoding table */
  227. long    horiz_mode_prefix[MAX_HORIZPREFIX];
  228.         /* color of next run, BLACK or WHITE */
  229. char    horiz_mode_color[MAX_HORIZPREFIX];
  230. short    horiz_mode_prefix_count = 0;
  231.  
  232. static    unsigned char bit_mask[8] =
  233.     { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
  234.  
  235. #if USE_PROTOTYPES
  236. void    build_null_mode_tables(void);
  237. short    find_horiz_mode_prefix(long, char);
  238. short    find_null_mode_prefix(long);
  239. short    null_mode_type(long);
  240. void    build_horiz_mode_tables(void);
  241. short    horiz_mode_code_black(short);
  242. short    horiz_mode_code_white(short);
  243. void    build_uncomp_mode_tables(void);
  244. void    write_tables(FILE*);
  245. #else
  246. void    build_null_mode_tables();
  247. short    find_horiz_mode_prefix();
  248. short    find_null_mode_prefix();
  249. short    null_mode_type();
  250. void    build_horiz_mode_tables();
  251. short    horiz_mode_code_black();
  252. short    horiz_mode_code_white();
  253. void    build_uncomp_mode_tables();
  254. void    write_tables();
  255. #endif
  256.  
  257. int    verbose = FALSE;
  258. char    *storage_class = "";
  259. int    packoutput = TRUE;
  260.  
  261. void
  262. DECLARE2(main, int, argc, char**, argv)
  263. {
  264.     while (argc > 1 && argv[1][0] == '-') {
  265.     if (strcmp(argv[1], "-v") == 0) {
  266.         verbose = TRUE;
  267.         argc--, argv++;
  268.     } else if (strcmp(argv[1], "-c") == 0) {
  269.         storage_class = "const ";
  270.         argc--, argv++;
  271.     } else if (strcmp(argv[1], "-p") == 0) {
  272.         packoutput = FALSE;
  273.         argc--, argv++;
  274.     }
  275.     }
  276.     build_null_mode_tables();        /* null mode decoding tables */
  277.     if (verbose) {
  278.     fprintf(stderr, "%d null mode prefixes defined\n",
  279.         (int) null_mode_prefix_count);
  280.     fprintf(stderr, "building uncompressed mode scripts...\n");
  281.     }
  282.     build_uncomp_mode_tables();        /* uncompressed mode decoding tables */
  283.     if (verbose) {
  284.     fprintf(stderr, "%d uncompressed mode prefixes defined\n",
  285.         (int) uncomp_mode_prefix_count);
  286.     fprintf(stderr, "building 1D scripts...\n");
  287.     }
  288.     build_horiz_mode_tables();        /* 1D decoding tables */
  289.     if (verbose)
  290.     fprintf(stderr, "%d incomplete prefixes defined\n",
  291.         (int) horiz_mode_prefix_count);
  292.     write_tables(stdout);
  293.     exit(0);
  294. }
  295.  
  296. void
  297. DECLARE3(write_null_mode_table, FILE*, fd, NullModeTable, table, char*, name)
  298. {
  299.     int i, j, lastNonZero;
  300.     char* outersep;
  301.     char* sep;
  302.  
  303.     fprintf(fd, "%su_char\t%s[%d][256] = {", storage_class,
  304.     name, (int) null_mode_prefix_count);
  305.     outersep = "";
  306.     if (!packoutput) {
  307.     for (i = 0; i < null_mode_prefix_count; i++) {
  308.         fprintf(fd, "%s\n/* prefix %d */ {\n", outersep, i);
  309.         sep = "    ";
  310.         for (j = 0; j < 256; j++) {
  311.         fprintf(fd, "%s%2d", sep, (int) table[i][j]);
  312.         if (((j+1) % 16) == 0) {
  313.             fprintf(fd, ", /* %3d-%3d */\n", j-15, j);
  314.             sep = "    ";
  315.         } else
  316.             sep = ",";
  317.         }
  318.         fprintf(fd, "}");
  319.         outersep = ",";
  320.     }
  321.     } else {
  322.     for (i = 0; i < null_mode_prefix_count; i++) {
  323.         fprintf(fd, "%s{\n", outersep);
  324.         for (j = 255; j > 0; j--)
  325.         if (table[i][j] != 0)
  326.             break;
  327.         sep = "";
  328.         lastNonZero = j;
  329.         for (j = 0; j <= lastNonZero; j++) {
  330.         fprintf(fd, "%s%d", sep, (int) table[i][j]);
  331.         if (((j+1) % 24) == 0)
  332.             putc('\n', fd);
  333.         sep = ",";
  334.         }
  335.         fprintf(fd, "}");
  336.         outersep = ",";
  337.     }
  338.     }
  339.     fprintf(fd, "\n};\n");
  340. }
  341.  
  342. void
  343. DECLARE3(write_horiz_mode_table, FILE*, fd, HorizModeTable, table, char*, name)
  344. {
  345.     int i, j, lastNonZero;
  346.     char* outersep;
  347.     char* sep;
  348.  
  349.     fprintf(fd, "%s u_char\t%s[%d][256] = {", storage_class,
  350.     name, (int) horiz_mode_prefix_count);
  351.     outersep = "";
  352.     if (!packoutput) {
  353.     for (i = 0; i < horiz_mode_prefix_count; i++) {
  354.         fprintf(fd, "%s\n/* prefix %d */ {\n", outersep, i);
  355.         sep = "    ";
  356.         for (j = 0; j < 256; j++) {
  357.         fprintf(fd, "%s%3d", sep, (int) table[i][j]);
  358.         if (((j+1) % 14) == 0) {
  359.             fprintf(fd, ", /* %3d-%3d */\n", j-13, j);
  360.             sep = "    ";
  361.         } else
  362.             sep = ",";
  363.         }
  364.         fprintf(fd, "\n}");
  365.         outersep = ",";
  366.     }
  367.     } else {
  368.     outersep = "";
  369.     for (i = 0; i < horiz_mode_prefix_count; i++) {
  370.         fprintf(fd, "%s{\n", outersep);
  371.         for (j = 255; j > 0; j--)
  372.         if (table[i][j] != 0)
  373.             break;
  374.         sep = "";
  375.         lastNonZero = j;
  376.         for (j = 0; j <= lastNonZero; j++) {
  377.         fprintf(fd, "%s%d", sep, (int) table[i][j]);
  378.         if (((j+1) % 24) == 0)
  379.             putc('\n', fd);
  380.         sep = ",";
  381.         }
  382.         fprintf(fd, "}");
  383.         outersep = ",";
  384.     }
  385.     }
  386.     fprintf(fd, "\n};\n");
  387. }
  388.  
  389. void
  390. write_define(fd, name, value, comment)
  391.     FILE *fd;
  392.     char *name;
  393.     int value;
  394.     char *comment;
  395. {
  396.     fprintf(fd, "#define\t%s\t%d", name, value);
  397.     if (!packoutput && comment)
  398.     fprintf(fd, "\t/* %s */", comment);
  399.     fprintf(fd, "\n");
  400. }
  401.  
  402. void
  403. write_preamble(fd)
  404.     FILE *fd;
  405. {
  406.     fprintf(fd, "%s\n",
  407. "/* DO NOT EDIT THIS FILE, IT WAS AUTOMATICALLY CREATED BY mkg3state */");
  408.     write_define(fd, "ACT_INCOMP", ACT_INCOMP, "incompletely decoded code");
  409.     write_define(fd, "ACT_INVALID", ACT_INVALID, "invalide code");
  410.     write_define(fd, "ACT_WRUNT", ACT_WRUNT, "terminating white run code");
  411.     write_define(fd, "ACT_WRUN", ACT_WRUN, "non-terminating white run code");
  412.     write_define(fd, "ACT_BRUNT", ACT_BRUNT, "terminating black run code");
  413.     write_define(fd, "ACT_BRUN", ACT_BRUN, "non-terminating black run code");
  414.     write_define(fd, "ACT_EOL", ACT_EOL, "end-of-line code");
  415.     fprintf(fd, "\n");
  416.     fprintf(fd, "/* modes that the decoder can be in */\n");
  417.     write_define(fd, "MODE_NULL", MODE_NULL, NULL);
  418.     write_define(fd, "MODE_PASS", MODE_PASS, NULL);
  419.     write_define(fd, "MODE_HORIZ", MODE_HORIZ, NULL);
  420.     write_define(fd, "MODE_VERT_V0", MODE_VERT_V0, NULL);
  421.     write_define(fd, "MODE_VERT_VR1", MODE_VERT_VR1, NULL);
  422.     write_define(fd, "MODE_VERT_VR2", MODE_VERT_VR2, NULL);
  423.     write_define(fd, "MODE_VERT_VR3", MODE_VERT_VR3, NULL);
  424.     write_define(fd, "MODE_VERT_VL1", MODE_VERT_VL1, NULL);
  425.     write_define(fd, "MODE_VERT_VL2", MODE_VERT_VL2, NULL);
  426.     write_define(fd, "MODE_VERT_VL3", MODE_VERT_VL3, NULL);
  427.     write_define(fd, "MODE_UNCOMP", MODE_UNCOMP, NULL);
  428.     write_define(fd, "MODE_ERROR", MODE_ERROR, NULL);
  429.     write_define(fd, "MODE_ERROR_1", MODE_ERROR_1, NULL);
  430.     fprintf(fd, "\n");
  431.     fprintf(fd, "#define\tRUNLENGTH(ix)    (TIFFFaxWhiteCodes[ix].runlen)\n");
  432.     fprintf(fd, "\n");
  433.     write_define(fd, "UNCOMP_INCOMP", UNCOMP_INCOMP, NULL);
  434.     fprintf(fd, "/* runs of [0]*1 */\n");
  435.     write_define(fd, "UNCOMP_RUN0", UNCOMP_RUN0, NULL);
  436.     write_define(fd, "UNCOMP_RUN1", UNCOMP_RUN1, NULL);
  437.     write_define(fd, "UNCOMP_RUN2", UNCOMP_RUN2, NULL);
  438.     write_define(fd, "UNCOMP_RUN3", UNCOMP_RUN3, NULL);
  439.     write_define(fd, "UNCOMP_RUN4", UNCOMP_RUN4, NULL);
  440.     write_define(fd, "UNCOMP_RUN5", UNCOMP_RUN5, NULL);
  441.     write_define(fd, "UNCOMP_RUN6", UNCOMP_RUN6, NULL);
  442.     fprintf(fd, "/* runs of [0]* w/ terminating color */\n");
  443.     write_define(fd, "UNCOMP_TRUN0", UNCOMP_TRUN0, NULL);
  444.     write_define(fd, "UNCOMP_TRUN1", UNCOMP_TRUN1, NULL);
  445.     write_define(fd, "UNCOMP_TRUN2", UNCOMP_TRUN2, NULL);
  446.     write_define(fd, "UNCOMP_TRUN3", UNCOMP_TRUN3, NULL);
  447.     write_define(fd, "UNCOMP_TRUN4", UNCOMP_TRUN4, NULL);
  448.     fprintf(fd, "/* special code for unexpected EOF */\n");
  449.     write_define(fd, "UNCOMP_EOF", UNCOMP_EOF, NULL);
  450.     fprintf(fd, "/* invalid code encountered */\n");
  451.     write_define(fd, "UNCOMP_INVALID", UNCOMP_INVALID, NULL);
  452.     fprintf(fd, "/* codes >= terminate uncompress mode */\n");
  453.     fprintf(fd, "#define\tUNCOMP_EXIT    UNCOMP_TRUN0\n");
  454.     fprintf(fd, "\n");
  455. }
  456.  
  457. void
  458. extern_table(fd, name)
  459.     FILE* fd;
  460.     char* name;
  461. {
  462.     fprintf(fd, "extern\t%su_char %s[][256];\n", storage_class, name);
  463. }
  464.  
  465. void
  466. write_tables(fd)
  467.     FILE* fd;
  468. {
  469.     write_preamble(fd);
  470.     fprintf(fd, "#ifdef G3STATES\n");
  471.     write_null_mode_table(fd, null_mode, "TIFFFax2DMode");
  472.     write_null_mode_table(fd, null_mode_next_state, "TIFFFax2DNextState");
  473.     write_null_mode_table(fd, uncomp_mode, "TIFFFaxUncompAction");
  474.     write_null_mode_table(fd, uncomp_mode_next_state, "TIFFFaxUncompNextState");
  475.     write_horiz_mode_table(fd, horiz_mode, "TIFFFax1DAction");
  476.     write_horiz_mode_table(fd, horiz_mode_next_state, "TIFFFax1DNextState");
  477.     fprintf(fd, "#else\n");
  478.     extern_table(fd, "TIFFFax2DMode");
  479.     extern_table(fd, "TIFFFax2DNextState");
  480.     extern_table(fd, "TIFFFaxUncompAction");
  481.     extern_table(fd, "TIFFFaxUncompNextState");
  482.     extern_table(fd, "TIFFFax1DAction");
  483.     extern_table(fd, "TIFFFax1DNextState");
  484.     fprintf(fd, "#endif\n");
  485. }
  486.  
  487. short
  488. DECLARE1(find_null_mode_prefix, long, prefix)
  489. {
  490.     short j1;
  491.  
  492.     if (prefix == 0L)
  493.     return (0);
  494.     for (j1 = 8; j1 < null_mode_prefix_count; j1++)
  495.     if (prefix == null_mode_prefix[j1])
  496.         return (j1);
  497.     if (null_mode_prefix_count == MAX_NULLPREFIX) {
  498.     fprintf(stderr, "ERROR: null mode prefix table overflow\n");
  499.     exit(1);
  500.     }
  501.     if (verbose)
  502.     fprintf(stderr, "adding null mode prefix[%d] 0x%lx\n",
  503.         (int) null_mode_prefix_count, prefix);
  504.     null_mode_prefix[null_mode_prefix_count++] = prefix;
  505.     return (null_mode_prefix_count-1);
  506. }
  507.  
  508. short
  509. DECLARE2(find_horiz_mode_prefix, long, prefix, char, color)
  510. {
  511.     short j1;
  512.  
  513.     for (j1 = 0; j1 < horiz_mode_prefix_count; j1++)
  514.     if (prefix == horiz_mode_prefix[j1] && horiz_mode_color[j1] == color)
  515.         return (j1);
  516.     /*
  517.      * It wasn't found, so add it to the tables, but first, is there room?
  518.      */
  519.     if (horiz_mode_prefix_count == MAX_HORIZPREFIX) {
  520.     fprintf(stderr, "ERROR: 1D prefix table overflow\n");
  521.     exit(1);
  522.     }
  523.     /* OK, there's room... */
  524.     if (verbose)
  525.     fprintf(stderr, "\nhoriz mode prefix %d, color %c = 0x%lx ",
  526.         (int) horiz_mode_prefix_count, "WB"[color], prefix);
  527.     horiz_mode_prefix[horiz_mode_prefix_count] = prefix;
  528.     horiz_mode_color[horiz_mode_prefix_count] = color;
  529.     horiz_mode_prefix_count++;
  530.     return (horiz_mode_prefix_count - 1);
  531. }
  532.  
  533. short
  534. DECLARE1(find_uncomp_mode_prefix, long, prefix)
  535. {
  536.     short j1;
  537.  
  538.     if (prefix == 0L)
  539.     return (0);
  540.     for (j1 = 8; j1 < uncomp_mode_prefix_count; j1++)
  541.     if (prefix == uncomp_mode_prefix[j1])
  542.         return (j1);
  543.     if (uncomp_mode_prefix_count == MAX_NULLPREFIX) {
  544.     fprintf(stderr, "ERROR: uncomp mode prefix table overflow\n");
  545.     exit(1);
  546.     }
  547.     if (verbose)
  548.     fprintf(stderr, "adding uncomp mode prefix[%d] 0x%lx\n",
  549.         (int) uncomp_mode_prefix_count, prefix);
  550.     uncomp_mode_prefix[uncomp_mode_prefix_count++] = prefix;
  551.     return (uncomp_mode_prefix_count-1);
  552. }
  553.  
  554. short
  555. DECLARE1(null_mode_type, long, prefix)
  556. {
  557.     switch (prefix) {
  558.     case 0x18000L: return (MODE_VERT_V0);    /* 1 */
  559.     case 0x36000L: return (MODE_VERT_VR1);    /* 011 */
  560.     case 0x34000L: return (MODE_VERT_VL1);    /* 010 */
  561.     case 0x32000L: return (MODE_HORIZ);        /* 001 */
  562.     case 0x41000L: return (MODE_PASS);        /* 0001 */
  563.     case 0x60C00L: return (MODE_VERT_VR2);    /* 0000 11 */
  564.     case 0x60800L: return (MODE_VERT_VL2);    /* 0000 10 */
  565.     case 0x70600L: return (MODE_VERT_VR3);    /* 0000 011 */
  566.     case 0x70400L: return (MODE_VERT_VL3);    /* 0000 010 */
  567.     case 0x80200L: return (MODE_ERROR);        /* 0000 0010 */
  568.     case 0x90300L: return (MODE_ERROR);        /* 0000 0011 0 */
  569.     case 0xA0380L: return (MODE_ERROR);        /* 0000 0011 10 */
  570.     case 0xA03C0L: return (MODE_UNCOMP);    /* 0000 0011 11 */
  571.     /*
  572.      * Under the assumption that there are no
  573.      * errors in the file, then this bit string
  574.      * can only be the beginning of an EOL code.
  575.      */
  576.     case 0x70000L: return (MODE_ERROR_1);    /* 0000 000 */
  577.     }
  578.     return (-1);
  579. }
  580.  
  581. short
  582. DECLARE1(uncomp_mode_type, long, prefix)
  583. {
  584.     short code;
  585.     short len;
  586.     switch (prefix) {
  587.     case 0x18000L: return (UNCOMP_RUN1);    /* 1 */
  588.     case 0x24000L: return (UNCOMP_RUN2);    /* 01 */
  589.     case 0x32000L: return (UNCOMP_RUN3);    /* 001 */
  590.     case 0x41000L: return (UNCOMP_RUN4);    /* 0001 */
  591.     case 0x50800L: return (UNCOMP_RUN5);    /* 0000 1 */
  592.     case 0x60400L: return (UNCOMP_RUN6);    /* 0000 01 */
  593.     case 0x70200L: return (UNCOMP_TRUN0);    /* 0000 001 */
  594.     case 0x80100L: return (UNCOMP_TRUN1);    /* 0000 0001 */
  595.     case 0x90080L: return (UNCOMP_TRUN2);    /* 0000 0000 1 */
  596.     case 0xA0040L: return (UNCOMP_TRUN3);    /* 0000 0000 01 */
  597.     case 0xB0020L: return (UNCOMP_TRUN4);    /* 0000 0000 001 */
  598.     }
  599.     code = prefix & 0xffffL;
  600.     len = (prefix >> 16) & 0xf;
  601.     return ((code || len > 10) ? UNCOMP_INVALID : -1);
  602. }
  603.  
  604. #define    BASESTATE(b)    ((unsigned char) ((b) & 0x7))
  605.  
  606. void
  607. build_null_mode_tables()
  608. {
  609.     short prefix;
  610.  
  611.     /*
  612.      * Note: the first eight entries correspond to
  613.      * a null prefix and starting bit numbers 0-7.
  614.      */
  615.     null_mode_prefix_count = 8;
  616.     for (prefix = 0; prefix < null_mode_prefix_count; prefix++) {
  617.     short byte;
  618.     for (byte = 0; byte < 256; byte++) {
  619.         short firstbit;
  620.         short bit;
  621.         long curprefix;
  622.         char found_code = FALSE;
  623.  
  624.         if (prefix < 8) {
  625.         curprefix = 0L;
  626.         firstbit = prefix;
  627.         } else {
  628.         curprefix = null_mode_prefix[prefix];
  629.         firstbit = 0;
  630.         }
  631.         for (bit = firstbit; bit < 8 && !found_code; bit++) {
  632.         short mode;
  633.  
  634.         if (bit_mask[bit] & byte)
  635.             curprefix = append_1(curprefix);
  636.         else
  637.             curprefix = append_0(curprefix);
  638.         switch (mode = null_mode_type(curprefix)) {
  639.         case MODE_PASS:
  640.         case MODE_HORIZ:
  641.         case MODE_VERT_V0:
  642.         case MODE_VERT_VR1:
  643.         case MODE_VERT_VR2:
  644.         case MODE_VERT_VR3:
  645.         case MODE_VERT_VL1:
  646.         case MODE_VERT_VL2:
  647.         case MODE_VERT_VL3:
  648.         case MODE_UNCOMP:
  649.         case MODE_ERROR:
  650.         case MODE_ERROR_1:
  651.             /*
  652.              * NOTE: if the bit number is 8, then the table
  653.              * entry will be zero, which indicates a new byte
  654.              * is to be fetched during the decoding process
  655.              */
  656.             found_code = TRUE;
  657.             null_mode[prefix][byte] = (unsigned char) mode;
  658.             null_mode_next_state[prefix][byte] = BASESTATE(bit+1);
  659.             break;
  660.         }
  661.         }
  662.         if (!found_code) {
  663.         null_mode_next_state[prefix][byte] = (unsigned char)
  664.             find_null_mode_prefix(curprefix);
  665.         /*
  666.          * This indicates to the decoder that
  667.          * no valid code has yet been identified.
  668.          */
  669.         null_mode[prefix][byte] = MODE_NULL;
  670.         }
  671.     }
  672.     }
  673. }
  674.  
  675. void
  676. build_horiz_mode_tables()
  677. {
  678.     unsigned short byte;
  679.     short prefix;
  680.  
  681.     /*
  682.      * The first 8 are for white,
  683.      * the second 8 are for black,
  684.      * beginning with bits 0-7.
  685.      */
  686.     horiz_mode_prefix_count = 16;
  687.     for (prefix = 0; prefix < horiz_mode_prefix_count; prefix++)
  688.     for (byte = 0; byte < 256; byte++) {
  689.         short bits_digested = 0;
  690.         short bit;
  691.         short firstbit;
  692.         char color;
  693.         unsigned long curprefix;
  694.  
  695.         if (prefix < 8) {
  696.         color = WHITE;
  697.         curprefix = 0L;
  698.         firstbit = prefix;
  699.         } else if (prefix < 16) {
  700.         color = BLACK;
  701.         curprefix = 0L;
  702.         firstbit = prefix - 8;
  703.         } else {
  704.         color = horiz_mode_color[prefix];
  705.         curprefix = horiz_mode_prefix[prefix];
  706.         firstbit = 0;
  707.         }
  708.         for (bit = firstbit; bit < 8 && !bits_digested; bit++) {
  709.         if (bit_mask[bit] & byte)
  710.             curprefix = append_1(curprefix);
  711.         else
  712.             curprefix = append_0(curprefix);
  713.         /*
  714.          * The following conversion allows for arbitrary strings of
  715.          * zeroes to precede the end-of-line code 0000 0000 0001.
  716.          * It assumes no errors in the data, and is based on
  717.          * the assumption that the code replaced (12 consecutive
  718.          * zeroes) can only be "legally" encountered before the
  719.          * end-of-line code.  This assumption is valid only for
  720.          * a Group 3 image; the combination will never occur
  721.          * in horizontal mode in a proper Group 4 image.
  722.          */
  723.         if (curprefix == 0xC0000L)
  724.             curprefix = 0xB0000L;
  725.         if (color == WHITE) {
  726.             short runlength = white_run_length(curprefix);
  727.  
  728.             if (runlength == G3CODE_INVALID) {
  729.             horiz_mode[prefix][byte] = (unsigned char) ACT_INVALID;
  730.             horiz_mode_next_state[prefix][byte] = (unsigned char) bit;
  731.             bits_digested = bit + 1;
  732.             } else if (runlength == G3CODE_EOL) { /* Group 3 only */
  733.             horiz_mode[prefix][byte] = (unsigned char) ACT_EOL;
  734.             horiz_mode_next_state[prefix][byte] = BASESTATE(bit+1);
  735.             bits_digested = bit + 1;
  736.             } else if (runlength != G3CODE_INCOMP) {
  737.             horiz_mode[prefix][byte] = (unsigned char)
  738.                 horiz_mode_code_white(runlength);
  739.             horiz_mode_next_state[prefix][byte] = BASESTATE(bit+1);
  740.             bits_digested = bit + 1;
  741.             }
  742.         } else {        /* color == BLACK */
  743.             short runlength = black_run_length(curprefix);
  744.  
  745.             if (runlength == G3CODE_INVALID) {
  746.             horiz_mode[prefix][byte] = (unsigned char) ACT_INVALID;
  747.             horiz_mode_next_state[prefix][byte] = (unsigned char) (bit+8);
  748.             bits_digested = bit + 1;
  749.             } else if (runlength == G3CODE_EOL) { /* Group 3 only */
  750.             horiz_mode[prefix][byte] = (unsigned char) ACT_EOL;
  751.             horiz_mode_next_state[prefix][byte] = BASESTATE(bit+1);
  752.             bits_digested = bit + 1;
  753.             } else if (runlength != G3CODE_INCOMP) {
  754.             horiz_mode[prefix][byte] = (unsigned char)
  755.                 horiz_mode_code_black(runlength);
  756.             horiz_mode_next_state[prefix][byte] = BASESTATE(bit+1);
  757.             bits_digested = bit + 1;
  758.             }
  759.         }
  760.         }
  761.         if (!bits_digested) {    /* no codewords after examining byte */
  762.         horiz_mode[prefix][byte] = (unsigned char) ACT_INCOMP;
  763.         horiz_mode_next_state[prefix][byte] = (unsigned char)
  764.             find_horiz_mode_prefix(curprefix, color);
  765.         }
  766.     }
  767. }
  768.  
  769. void
  770. build_uncomp_mode_tables()
  771. {
  772.     short prefix;
  773.  
  774.     /*
  775.      * Note: the first eight entries correspond to
  776.      * a null prefix and starting bit numbers 0-7.
  777.      */
  778.     uncomp_mode_prefix_count = 8;
  779.     for (prefix = 0; prefix < uncomp_mode_prefix_count; prefix++) {
  780.     short byte;
  781.     for (byte = 0; byte < 256; byte++) {
  782.         short firstbit;
  783.         short bit;
  784.         long curprefix;
  785.         char found_code = FALSE;
  786.  
  787.         if (prefix < 8) {
  788.         curprefix = 0L;
  789.         firstbit = prefix;
  790.         } else {
  791.         curprefix = uncomp_mode_prefix[prefix];
  792.         firstbit = 0;
  793.         }
  794.         for (bit = firstbit; bit < 8 && !found_code; bit++) {
  795.         short mode;
  796.  
  797.         if (bit_mask[bit] & byte)
  798.             curprefix = append_1(curprefix);
  799.         else
  800.             curprefix = append_0(curprefix);
  801.         mode = uncomp_mode_type(curprefix);
  802.         if (mode != -1) {
  803.             /*
  804.              * NOTE: if the bit number is 8, then the table
  805.              * entry will be zero, which indicates a new byte
  806.              * is to be fetched during the decoding process
  807.              */
  808.             found_code = TRUE;
  809.             uncomp_mode[prefix][byte] = (unsigned char) mode;
  810.             uncomp_mode_next_state[prefix][byte] = BASESTATE(bit+1);
  811.             break;
  812.         }
  813.         }
  814.         if (!found_code) {
  815.         uncomp_mode_next_state[prefix][byte] = (unsigned char)
  816.             find_uncomp_mode_prefix(curprefix);
  817.         /*
  818.          * This indicates to the decoder that
  819.          * no valid code has yet been identified.
  820.          */
  821.         uncomp_mode[prefix][byte] = UNCOMP_INCOMP;
  822.         }
  823.     }
  824.     }
  825. }
  826.